$(function() {
	//触发事件
	var tab = {
		tabAdd: function(title, url, id) {
			//新增一个Tab项
			element.tabAdd('xbs_tab', {
				title: title,
				content: '<iframe tab-id="' + id + '" frameborder="0" src="' + url + '" scrolling="yes" class="x-iframe"></iframe>',
				id: id
			})
		},
		tabDelete: function(othis) {
			//删除指定Tab项
			element.tabDelete('xbs_tab', '44'); //删除：“商品管理”

			othis.addClass('layui-btn-disabled');
		},
		tabChange: function(id) {
			//切换到指定Tab项
			element.tabChange('xbs_tab', id); //切换到：用户管理
		}
	};


//多选表单
	tableCheck = {
		init: function() {
			$(".layui-form-checkbox").click(function(event) {
				if($(this).hasClass('layui-form-checked')) {
					$(this).removeClass('layui-form-checked');
					if($(this).hasClass('header')) {
						$(".layui-form-checkbox").removeClass('layui-form-checked');
					}
				} else {
					$(this).addClass('layui-form-checked');
					if($(this).hasClass('header')) {
						$(".layui-form-checkbox").addClass('layui-form-checked');
					}
				}

			});
		},
		getData: function() {
			var obj = $(".layui-form-checked").not('.header');
			var arr = [];
			obj.each(function(index, el) {
				arr.push(obj.eq(index).attr('data-id'));
			});
			return arr;
		}
	}

	//开启表格多选
	tableCheck.init();


	//左侧菜单数据
	//cookie 取值
	var nav = $.cookie("nav");
	var navs = JSON.parse(nav);
	var navid = navs.id;
	var Wtid = navs.w;
	var userName = navs.userName;　　
	//判断父ID 取file路径
	var file = '';
	if(navid == 1){
		file = 'labourservices/';//劳务模块
	}else if(navid == 2){
		file = 'equipment/';//设备模块
	}else if(navid == 3){
		file = 'materiel/';//物料模块
	}else if(navid == 5){
		file = 'environment/';//环境模块
	}else if(navid == 71){
		file = 'user/';//权限模块
	}else if(navid == '879a8009-0028-44a5-a714-0d2d29b8be91'){
		file = 'datacenter/';//大数据中心模块
	};
	
	//请求地址
	var navurl = jurisdictionport + "/AuthorityController/getMenu";
	//发送get请求数据获取menu
	var getNavData = (function() {
		$.get(navurl, {
			username: userName,
			parentid: navid,
		}, function(result) {
			var code = result.code;
			if(code == 0) {
				var menu = result.object;
				$.each(menu, function(idx, obj) {
					var idx = idx + 1;
					var listhtml = '<a  _href="../models/'+file+obj.addr+'">\
										<i class="iconfont">&#xe6b8;</i>\
										<cite>' + obj.name + '</cite>\
										<i class="iconfont nav_right">&#xe673;</i>\
									</a>';             
					var isend = obj.isend;
					if(isend == 0) {
						var child = obj.children;
						var html = '<li id="' + obj.resourceid + '">' + listhtml + '</li>';
						$("#nav").append(html);
						$.each(child, function(idx, child) {
							var idx = idx + 1;
							var Nexthtml = '<ul class="sub-menu">\
										<li>\
											<a _href="../models/'+file+child.addr+'">\
												<i class="iconfont">&#xe6b8;</i>\
												<cite>' + child.name + '</cite>\
											</a>\
										<li>\
									</ul>';
							$("#" + obj.resourceid).append(Nexthtml);							
						});
					} else {
						var html = '<li id="' + obj.resourceid + '">' + listhtml + '</li>';
						$("#nav").append(html);
					}
					$(".username").text(userName);
				}, 'JSON')
			} else {
				alert("您的登录认证已失效，请重新登录！");
				location.href = "login.html";
			}
			navopen();
		})
	});
	
	getNavData();

	//左侧菜单下拉效果
	var navopen = (function() {
		$('.left-nav #nav li').click(function(event) {
			var id2 = $(this).attr('id');
			if($("#" + id2).children('.sub-menu').length) {
				if($(this).children('.sub-menu').css('display', 'block')) {
					$(this).find('.nav_right').html('&#xe685;');
					$(this).children('.sub-menu').stop().slideDown();
					$(this).siblings().children('.sub-menu').stop().slideUp();
				} else {
					$(this).children('.sub-menu').stop().slideDown();
					$(this).siblings().children('.sub-menu').stop().slideUp();
					$(this).siblings().find('.nav_right').html('&#xe685;');
				}
			} else {
				var url = $(this).children('a').attr('_href');
				var title = $(this).find('cite').html();
				var index = $('.left-nav #nav li').index($(this));
				for(var i = 0; i < $('.x-iframe').length; i++) {
					if($('.x-iframe').eq(i).attr('tab-id') == index + 1) {
						tab.tabChange(index + 1);
						event.stopPropagation();
						return;
					}
				};
				
				tab.tabAdd(title, url, index + 1);
				tab.tabChange(index + 1);
			}
			event.stopPropagation();

		})
	});
	
	
});